﻿using System;
using System.Web.UI.WebControls;

namespace MyHealthByDesign.Views
{
    /// <summary>
    /// Description:
    /// Dispalys and process logic for AHA questionnaire
    /// 
    /// Author: Sanjeev Shrestha
    /// Date Created: 10/31/2011
    /// 
    /// Other code files referenced:
    /// None.
    /// 
    /// Regression Test Information:
    /// None.
    /// 
    /// Revision History:
    /// 11/03/2011 Sanjeev Shrestha - Added logic for page.
   /// 
    /// 
    /// Known Faults:
    /// None.
    /// </summary>
    public partial class Aha : System.Web.UI.Page
    {
        Domain.Domain domain = new Domain.Domain();
        /// <summary>
        /// Page Load event handler - check if user is logged in
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// Page displayed
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!domain.IsUserLoggedIn())
            {
                Response.Redirect("../Default.aspx");
            }
        }

        /// <summary>
        /// Submit button handler
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// Redirects to appropriate page based on selections checked
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnSubmit_Click(object sender, EventArgs e)
        {
            string redirectPage = "AhaGoodToGo.aspx";

            if (chkBxNone.Checked)
            {
                redirectPage = "AhaGoodToGo.aspx";
            }
            else
            {
                //if the user selects one box under history, symptoms or other health
                //issues or two boxes under cardiovascular risk factors
                if (IsHistorySymptomsChecked()
                    || IsHealthIssuesChecked()
                    || IsCardiovascularChecked()
                    )
                {
                    redirectPage = "SeeDoctor.aspx";
                }
            }

            Response.Redirect(redirectPage);
        }

        /// <summary>
        /// Checks if History symptoms checked
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// returns boolean based on values checked
        /// </summary>
        /// <returns></returns>
        private bool IsHistorySymptomsChecked()
        {
            return chkBxHistory.SelectedIndex != -1 && chkBxSymptoms.SelectedIndex != -1;
        }

        /// <summary>
        /// Checks if health issues checked
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// returns boolean based on values checked
        /// </summary>
        /// <returns></returns>
        private bool IsHealthIssuesChecked()
        {
            return chkBxOther.SelectedIndex != -1;
        }

        /// <summary>
        /// Checks if cardiovascular issues checked
        /// 
        /// PreConditions:
        /// None
        /// 
        /// PostConditions:
        /// returns boolean based on values checked
        /// </summary>
        /// <returns></returns>
        private bool IsCardiovascularChecked()
        {
            bool isChecked = false;
            int count = 0;

            foreach (ListItem chkBxItem in chkBxCardiovascular.Items)
            {
                if (chkBxItem.Selected)
                {
                    count++;
                }
            }

            if (count >= 2)
            {
                isChecked = true;
            }

            return isChecked;
        }
    }
}